﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;
using System.Text;

namespace He.Web.manage.weixin
{
    public partial class keyword_list : Manage
    {
        protected int pageSize = 12;                   //每页记录数
        protected int page;                            //当前页
        protected int pcount;                          //总页数
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);

            if (!IsPostBack)
            {
                chkLoginLevel("viewVxkeyword");
                CookieHelper.Set("LastListUrl", Request.RawUrl);    //保存最后一次访问的列表页Url
                RptBind();
            }
        }


        #region 数据绑定
        private void RptBind()
        {
            BLL.wx_keyword bll = new BLL.wx_keyword();
            rptList.DataSource = bll.GetPageList(pageSize, page, "id>0", "sort_id asc,id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount("id>0");
        }
        #endregion

        public string GetAnswer(int id)
        {
            BLL.wx_keyword bll = new BLL.wx_keyword();
            BLL.article abll = new BLL.article();
            Model.wx_keyword model = bll.GetModel(id);
            StringBuilder strTemp = new StringBuilder();
            switch (model.msg_type)
            {
                case "text":
                    strTemp.Append(model.answer);
                    break;
                case "news":
                    string[] idArr = model.article_id.Split(',');
                    if (idArr.Length > 0)
                    {
                        foreach (string s in idArr)
                        {
                            int aid = Convert.ToInt32(s);
                            if (abll.Exists(aid))
                            {
                                Model.article article = abll.GetModel(aid);
                                strTemp.AppendFormat("<li id=\"row{1}\" style=\"height:25px;\">{0}</li>", article.title, article.id);
                            }
                        }
                    }
                    break;
            }
            return strTemp.ToString();
        }

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delWxkeyword");
            BLL.wx_keyword bll = new BLL.wx_keyword();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                Model.wx_keyword model = bll.GetModel(id);
                if (cb.Checked)
                {
                    bll.Delete(id);
                }
            }
            SaveLogs("[微信关键词管理]批量删除");
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editWxkeyword");
            BLL.wx_keyword bll = new BLL.wx_keyword();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs("[微信关键词管理]批量保存排序");
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }


    }
}